cd "/Users/dawnshen/Dropbox/Yinzhi/MassShootings/Election"
****************************
****************************
** Presidential election national level analysis
****************************
****************************

use "Election_working_analysisfile",clear

*************************************************************************
* Generate indicators of interview timing 
*************************************************************************
* Drop obs if being interviewed on the election day
drop if date==pedate

gen bf28=0
replace bf28=1 if date-pedate>=-28 & date-pedate<=-1 //interviewed in the four weeks before the incident - control group
gen r_af1=0 //interviewed one day after the incident
replace r_af1=1 if date-pedate==1
gen bf28_1=0
replace bf28_1=1 if r_af1==1 | bf28==1  //interviewed one day after the incident or in the control group


foreach i in 2 4 7 14 21 28 {
gen r_af`i'=0
replace r_af`i'=1 if date-pedate>=`i'-1 & date-pedate<=`i'+1  //interviewed around i days after the incident

gen bf28_`i'=0
replace bf28_`i'=1 if bf28==1 | r_af`i'==1 //interviewed around i days after the incident or in the control group
replace bf28_`i'=. if date-pedate>0 & date-pedate<`i'-1 //remove those interviewed after the incident and before i-1 days from the model
}

* # of obs interviewed on the (i-1)th, ith, and (i+1)th day since election date
foreach i in 1 2 4 7 14 21 28 {
tab1 r_af`i', m
}

*************************************************************************
** Graph effect size
*************************************************************************
mdesc polrep    //12% missing
mdesc poldem    //12% missing


tab1 r_af1 r_af2 r_af4 r_af7 r_af14 r_af21 r_af28 if polrep==1
tab1 r_af1 r_af2 r_af4 r_af7 r_af14 r_af21 r_af28 if poldem==1


** 2008 election		
foreach party in polrep poldem {
local rownames ""
mata: estimates = J(0,3,.)
foreach i in sadness anger happy smile {
  foreach j in 1 2 4 7 14 21 28 {  
	        quietly regress `i' r_af`j' bf28_`j' rwhite coll married female age i.kids i.year i.month i.dow if `party'==1 & election==1, level(90)
            mata: estimates = estimates \ st_matrix("r(table)")'[1, (1,5,6)]
            local rownames `rownames' `i'_`j'
    }
	}
mata: st_matrix("emotion_`party'", estimates)
matrix rownames emotion_`party' = `rownames'
matrix colnames emotion_`party' = Coef LL UL
matprint emotion_`party', d(3)
}

coefplot (matrix(emotion_poldem[,1]), ci((emotion_poldem[,2] emotion_poldem[,3])) msize(small) msymbol(circle) label("Democrat")) (matrix(emotion_polrep[,1]), ci((emotion_polrep[,2] emotion_polrep[,3])) msize(small) msymbol(diamond) ciopts(lpattern(dash)) label("Republican")), yline(0,lcolor(grey) lpattern(dash) lwidth(.3)) graphregion(color(white)) vertical ytitle("Effect size",size(small)) ylabel(-.2(0.02).2, labsize(vsmall) angle(horizontal)) ///
groups(sadness_* = Sadness anger_* = Anger worry_* = Worry happy_* = Happy smile_* = Smile) yscale(range(-.2(0.02).2)) xlabel(1 "day1" 2 "day2" 3 "day4" 4 "day7" 5 "day14" 6 "day21" 7 "day28" 9 "day1" 10 "day2" 11 "day4" 12 "day7" 13 "day14" 14 "day21" 15 "day28" 17 "day1" 18 "day2" 19 "day4" 20 "day7" 21 "day14" 22 "day21" 23 "day28" 25 "day1" 26 "day2" 27 "day4" 28 "day7" 29 "day14" 30 "day21" 31 "day28", labsize(vsmall) angle(vertical) )  
graph export 2008_nationbfaf_3day_controls_byemotion_byparty.pdf, replace



** 2012 election		
foreach party in polrep poldem {
local rownames ""
mata: estimates = J(0,3,.)
foreach i in sadness anger happy smile {
  foreach j in 1 2 4 7 14 21 28 {  
	        quietly regress `i' r_af`j' bf28_`j' rwhite coll married female age i.kids i.year i.month i.dow if `party'==1 & election==2, level(90)
            mata: estimates = estimates \ st_matrix("r(table)")'[1, (1,5,6)]
            local rownames `rownames' `i'_`j'
    }
	}
mata: st_matrix("emotion_`party'", estimates)
matrix rownames emotion_`party' = `rownames'
matrix colnames emotion_`party' = Coef LL UL
matprint emotion_`party', d(3)
}

coefplot (matrix(emotion_poldem[,1]), ci((emotion_poldem[,2] emotion_poldem[,3])) msize(small) msymbol(circle) label("Democrat")) (matrix(emotion_polrep[,1]), ci((emotion_polrep[,2] emotion_polrep[,3])) msize(small) msymbol(diamond) ciopts(lpattern(dash)) label("Republican")), yline(0,lcolor(grey) lpattern(dash) lwidth(.3)) graphregion(color(white)) vertical ytitle("Effect size",size(small)) ylabel(-.2(0.02).2, labsize(vsmall) angle(horizontal)) ///
groups(sadness_* = Sadness anger_* = Anger worry_* = Worry happy_* = Happy smile_* = Smile) yscale(range(-.2(0.02).2)) xlabel(1 "day1" 2 "day2" 3 "day4" 4 "day7" 5 "day14" 6 "day21" 7 "day28" 9 "day1" 10 "day2" 11 "day4" 12 "day7" 13 "day14" 14 "day21" 15 "day28" 17 "day1" 18 "day2" 19 "day4" 20 "day7" 21 "day14" 22 "day21" 23 "day28" 25 "day1" 26 "day2" 27 "day4" 28 "day7" 29 "day14" 30 "day21" 31 "day28", labsize(vsmall) angle(vertical) )  
graph export 2012_nationbfaf_3day_controls_byemotion_byparty.pdf, replace


** 2016 election		
foreach party in polrep poldem {
local rownames ""
mata: estimates = J(0,3,.)
foreach i in sadness anger happy smile {
  foreach j in 1 2 4 7 14 21 28 {  
	        quietly regress `i' r_af`j' bf28_`j' rwhite coll married female age i.kids i.year i.month i.dow if `party'==1 & election==3, level(90)
            mata: estimates = estimates \ st_matrix("r(table)")'[1, (1,5,6)]
            local rownames `rownames' `i'_`j'
    }
	}
mata: st_matrix("emotion_`party'", estimates)
matrix rownames emotion_`party' = `rownames'
matrix colnames emotion_`party' = Coef LL UL
matprint emotion_`party', d(3)
}

coefplot (matrix(emotion_poldem[,1]), ci((emotion_poldem[,2] emotion_poldem[,3])) msize(small) msymbol(circle) label("Democrat")) (matrix(emotion_polrep[,1]), ci((emotion_polrep[,2] emotion_polrep[,3])) msize(small) msymbol(diamond) ciopts(lpattern(dash)) label("Republican")), yline(0,lcolor(grey) lpattern(dash) lwidth(.3)) graphregion(color(white)) vertical ytitle("Effect size",size(small)) ylabel(-.2(0.02).2, labsize(vsmall) angle(horizontal)) ///
groups(sadness_* = Sadness anger_* = Anger worry_* = Worry happy_* = Happy smile_* = Smile) yscale(range(-.2(0.02).2)) xlabel(1 "day1" 2 "day2" 3 "day4" 4 "day7" 5 "day14" 6 "day21" 7 "day28" 9 "day1" 10 "day2" 11 "day4" 12 "day7" 13 "day14" 14 "day21" 15 "day28" 17 "day1" 18 "day2" 19 "day4" 20 "day7" 21 "day14" 22 "day21" 23 "day28" 25 "day1" 26 "day2" 27 "day4" 28 "day7" 29 "day14" 30 "day21" 31 "day28", labsize(vsmall) angle(vertical) )  
graph export 2016_nationbfaf_3day_controls_byemotion_byparty.pdf, replace


